package com.example.springboot_ruiji.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.constans.EmployeeConstans;
import com.example.springboot_ruiji.pojo.Category;
import com.example.springboot_ruiji.pojo.Dish;
import com.example.springboot_ruiji.pojo.DishDTO;
import com.example.springboot_ruiji.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;

    //查询
    @GetMapping("page")
    public R page(String name, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        /*String name1="%"+name+"%";*/
        return dishService.page(name,page,pageSize);
    }

    @PostMapping
    public R save(@RequestBody DishDTO dishDTO, HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDTO.setCreateUser(empId);
        dishDTO.setUpdateUser(empId);
        //2.把数据交给service
        dishService.save(dishDTO);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        DishDTO dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R updateDish(@RequestBody  DishDTO dishDto,HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        //2. 交给service
        dishService.updateDish(dishDto);
        return R.success("修改菜品成功");
    }

    //删除
    @DeleteMapping
    public R batchDelete(String ids){
        return dishService.batchDelete(ids);
    }

    @GetMapping("/list")
    public R list(Long categoryId){
        return dishService.list(categoryId);
    }

    @PostMapping("status/{status}")
    public R dishStatusByStatus(String ids,@PathVariable Integer status,HttpSession session){
        log.info("s:{}",status);
        Long id = (Long) session.getAttribute("employee");
        DishDTO dishDTO = new DishDTO();
        dishDTO.setUpdateUser(id);
        return dishService.dishStatusByStatus(dishDTO,ids,status);
    }
}










